package com.southminority.ethnic.controller.admin.language;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.annotation.RequireAdmin;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.admin.language.vo.*;
import com.southminority.ethnic.controller.language.vo.PageKeyReqVo;
import com.southminority.ethnic.pojo.LanguageNation;
import com.southminority.ethnic.pojo.MinorityLanguageCulture;
import com.southminority.ethnic.service.language.LanguageNationService;
import com.southminority.ethnic.service.language.MinorityLanguageCultureService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 民族分类控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "语言数据库管理-民族分类管理", description = "民族分类管理")
@RestController
@RequestMapping("/admin/language-nation")
@RequireAdmin(1)
public class LanguageNationAdminController {

    @Autowired
    private LanguageNationService service;

    /**
     * 分页查询民族分类v
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询民族分类", description = "管理员分页查询民族分类数据")
    public R<Page<LanguageNation>> page(@RequestBody PageKeyReqVo pageKeyReqVo) {
        try {
            log.info("分页查询民族分类{}",pageKeyReqVo.toString());
            Page<LanguageNation> result = service.selectOnesPageKey(
                pageKeyReqVo.getPageNum(), 
                pageKeyReqVo.getPageSize(), 
                pageKeyReqVo.getKey()
            );
            return R.ok(result);
        } catch (Exception e) {
            log.error("分页查询民族分类失败", e);
            return R.fail(e.getMessage());
        }
    }
    //获取所有民族分类
    @GetMapping("/get-all")
    @Operation(summary = "获取所有民族分类", description = "获取所有民族分类数据")
    public R<List<LanguageNation>> getAll() {
        try {
            log.info("获取所有民族分类");
            List<LanguageNation> result = service.selectAll();
            return R.ok(result);
        } catch (Exception e) {
            log.error("获取所有民族分类失败", e);
            return R.fail(e.getMessage());
        }
    }

    //获取根目录下的所有民族分类
    @GetMapping("/get-root-all")
    @Operation(summary = "获取所有根目录下的民族分类", description = "获取所有根目录下的民族分类数据")
    public R<List<LanguageNation>> getRootAll() {
        try {
            log.info("获取所有民族分类");
            List<LanguageNation> result = service.selectRootAll();
            return R.ok(result);
        } catch (Exception e) {
            log.error("获取所有民族分类失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据ID查询民族分类详情v
     */
    @PostMapping("/get-by-id")
    @Operation(summary = "获取民族分类详情", description = "根据ID获取民族分类详情")
    public R<LanguageNation> getById(@RequestBody LanguageNationGetByIdReqVo request) {
        try {
            log.info("获取民族分类详情{}",request.getId());
            LanguageNation result = service.selectOne(request.getId());
            return R.ok(result);
        } catch (Exception e) {
            log.error("获取民族分类详情失败", e);
            return R.fail(e.getMessage());
        }
    }



    /**
     * 新增民族分类v
     */
    @PostMapping("/add")
    @Operation(summary = "新增民族分类", description = "新增民族分类数据")
    public R<String> add(@Validated @RequestBody LanguageNationAddReqVo request) {
        try {
            LanguageNation culture = new LanguageNation();
            BeanUtils.copyProperties(request, culture);
            return R.result(service.addNation( culture));
        } catch (Exception e) {
            log.error("新增民族分类失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新民族分类v
     */
    @PutMapping("/update")
    @Operation(summary = "更新民族分类", description = "更新民族分类数据")
    public R<String> update(@Validated @RequestBody LanguageNationUpdateReqVo request) {
        try {
            return R.result(service.updateNationById(request.getId(), request.getNewCategoryName()));
        } catch (Exception e) {
            log.error("更新民族分类失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除民族分类v
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除民族分类", description = "根据ID删除民族分类数据")
    public R<String> delete(@Validated @RequestBody LanguageNationDeleteReqVo request) {
        try {
            return R.result(service.deleteNationById(request.getId()));
        } catch (Exception e) {
            log.error("删除民族分类失败", e);
            return R.fail(e.getMessage());
        }
    }

}
